var weekTotalVue = new Vue({
	el: "body",
	data: {
		isSearch: false,
		startYear: "",
		startWeek: "",
		endYear: "",
		endWeek: "",
		fridayObj: "", //保存本周周五的时间对象
		projectTotal: "", //用来保存工程档案的统计
		mapTotal: "", //用来保存测绘的任务统计
		lineTotal: "", //保存地下管线统计      
		lineZongBiaoSum: "",
		lineGuanXianSum: "",
		fileSearchTotal: "", //保存查询量统计
		fileTotal: "", //保存档案数量统计
		bookTotal: "",
		weekRange: "", //保存当前周的起始和结束
		isFastSelect: "", //判断是否点击了快速快捷键
		firstDate: "", //自选查询的起始值
		lastDate: "", //自选查询的结束值
		userid: "",
		leaderList: "", //保存所有的馆长名
		isLeader: false, //判断当前人是否是馆长
		otherTotal: "",
		fileTreeTotal: ""
	},
	created: function() {
		_this = this;
		_this.userid = getUrlItem("userid")
		$.getJSON("http://47.92.116.158:8099/BhGgjAPI/Supervise.asmx/getHeaderUsers_ProductValue?callback=?", function(jsondata) {
			console.log(jsondata)
			jsondata.forEach(function(item) {
				_this.leaderList += item.CODE + ","
			})
			_this.leaderList = _this.leaderList.substring(0, _this.leaderList.length - 1)
			_this.leaderList = _this.leaderList.split(",")

			for(var i = 0; i < _this.leaderList.length; i++) {
				if(_this.leaderList[i] == _this.userid) {
					_this.isLeader = true
				}
			}
		});

		//获取权限，判断能否查看测绘产值。

		if(getUrlItem("startYear") != "") {
			_this.startYear = getUrlItem("startYear")
			_this.startWeek = getUrlItem("startWeek")
			_this.endYear = getUrlItem("endYear")
			_this.endWeek = getUrlItem("endWeek")
			_this.isSearch = true
		} else {
			var time = new Date();
			var year = time.getFullYear(); //当前实际年月日
			var month = time.getMonth() + 1;
			var day = time.getDate();

			var nowWeek = getWeekNumber(year, month, day); //当前实际周数
			var weekRange = getFirstAndLastDay(time); //得到当前周的起始
			var fridayString = getFirstAndLastDay(time, "line").friday.replace(/-/g, "/");
			var nowFridayObj = new Date(fridayString); //当前周五的时间对象
			var nowFridayYear = nowFridayObj.getFullYear(); //当前周五的年月日
			var nowFridayMonth = nowFridayObj.getMonth() + 1;
			var nowFridayDay = nowFridayObj.getDate();

			_this.startYear = nowFridayYear;
			_this.startWeek = getWeekNumber(nowFridayYear, nowFridayMonth, nowFridayDay);
			_this.endYear = nowFridayYear;
			_this.endWeek = getWeekNumber(nowFridayYear, nowFridayMonth, nowFridayDay);

			_this.weekRange = getFirstAndLastDay(nowFridayObj) //当前周五在第几周就显示该周是第几周
			_this.fridayObj = nowFridayObj

			var nowFridayLimit = fridayString + " 17:00:00" //得到本周五的17：00时间对象   
			var nowFridayLimitObj = new Date(nowFridayLimit)

			if(time < nowFridayLimitObj) { //现在的日子如果没到周五17：00         显示的是上周的数据   
				var nowFridayTime = nowFridayObj.getTime();
				var oneDayLong = 24 * 60 * 60 * 1000;
				var realTime = nowFridayTime - 7 * oneDayLong
				var real = new Date(realTime)
				var realyear = real.getFullYear();
				var realmonth = real.getMonth() + 1;
				var realday = real.getDate();
				_this.startWeek = getWeekNumber(realyear, realmonth, realday);
				_this.endWeek = getWeekNumber(realyear, realmonth, realday);
				_this.startYear = realyear;
				_this.endYear = realyear

				_this.weekRange = getFirstAndLastDay(real)
			}
			_this.isSearch = false
		}

		searchProjectTotal(_this) //工程档案总数统计
		searchMapProductTotal(_this) //测绘总体款项总数统计
		searchLineTotal(_this) //地下管线总数统计
		searchSearchFileTotal(_this) //档案管理查询量总数统计
		searchBookTotal(_this) //馆内文书总数统计
		searchOtherTotal(_this) //其他项总数统计
		searchFileTreeTotal(_this) //档案管理树形总数统计

		_this.$nextTick(function() {
			setTimeout(function() {
				$(".statistics-total-body").removeClass("hidden")
				$(".loader").addClass("hidden")
			}, 300)
		})
	},
	ready: function() {
		$(function() {
			FastClick.attach(document.body);
		});
		// 禁止遮罩层的滑动事件
		$('.statistics-total-searchBar').bind("touchmove", function(e) {
			e.preventDefault();
		});

		//给遮罩层设置宽高
		var WindowHeight = $(window).height();
		var WindowWidth = $(window).width();
		$(".statistics-total-searchBar").css("height", WindowHeight);
		$(".statistics-total-searchBar").css("width", WindowWidth);

		//点击弹出遮罩层
		$(".selectBtn").click(function() {
			$(".statistics-total-searchBar").show();
			$(".statistics-total-searchBar").animate({ "right": 0 }, 250);
		})

		//初始化日历中组件
		$("#startDate").calendar();
		$("#endDate").calendar();

		//点击快捷键添加样式，重复点击取消样式
		$(".selectFastBtn div").click(function() {
			$(".selectFastBtn div").removeClass("active");
			if($(this).html() == "") {

			} else {
				$(this).addClass("active");
				if($(this).html() == _this.isFastSelect) {
					$(this).removeClass("active");
					_this.isFastSelect = "";
					_this.firstDate = "";
					_this.lastDate = "";
				} else {
					_this.isFastSelect = $(this).html();
				}
			}
		})
	},
	methods: {
		projectDetail: function(a) {

			location.href = "statisticsProject.html?startYear=" + _this.startYear + "&startWeek=" + _this.startWeek + "&endYear=" + _this.endYear + "&endWeek=" + _this.endWeek + "&type=" + a

		}, //工程档案详情
		mapDetail: function(a) {

			location.href = "statisticsMap.html?startYear=" + _this.startYear + "&startWeek=" + _this.startWeek + "&endYear=" + _this.endYear + "&endWeek=" + _this.endWeek + "&type=" + a

		}, //测绘详情
		lineDetail: function(a) {

			location.href = "statisticsLine.html?startYear=" + _this.startYear + "&startWeek=" + _this.startWeek + "&endYear=" + _this.endYear + "&endWeek=" + _this.endWeek + "&type=" + a

		}, //地下管线详情
		fileDetail: function(a) {

			location.href = "statisticsFile.html?startYear=" + _this.startYear + "&startWeek=" + _this.startWeek + "&endYear=" + _this.endYear + "&endWeek=" + _this.endWeek + "&type=" + a

		}, //档案管理详情
		bookDetail: function(a) {

			location.href = "statisticsBook.html?startYear=" + _this.startYear + "&startWeek=" + _this.startWeek + "&endYear=" + _this.endYear + "&endWeek=" + _this.endWeek + "&type=" + a

		},
		otherDetail: function(a) {

			location.href = "statisticsOther.html?startYear=" + _this.startYear + "&startWeek=" + _this.startWeek + "&endYear=" + _this.endYear + "&endWeek=" + _this.endWeek + "&type=" + a

		},
		back: function() {
			window.history.go(-1); //返回上一页
		},
		backTotal: function() {
			_this.firstDate = "";
			_this.lastDate = "";
			$(".selectFastBtn div").removeClass("active");
			$(".statistics-total-searchBar").animate({ "right": -600 }, 250);
			setTimeout(function() {
				$(".statistics-total-searchBar").hide();
			}, 250)
		},
		searchCycle: function() {
			var startDate = $("#startDate").val();
			var endDate = $("#endDate").val();
			//通过周期筛选数据
			if(startDate.length > 0 && endDate.length > 0) {
				if(startDate > endDate) {
					$.toast("查询周期无效", "cancel", function(toast) {});
				} else {
					var startTime = new Date(startDate); //筛选的开始时间对象
					var startFriday = new Date(getFirstAndLastDay(startTime, "line").friday); //筛选的开始时间所在周周五

					var endTime = new Date(endDate);
					var endFriday = new Date(getFirstAndLastDay(endTime, "line").friday); //筛选的结束时间所在周周五

					var startYear = startFriday.getFullYear();
					var startMonth = startFriday.getMonth() + 1;
					var startDay = startFriday.getDate();
					var startWeekNum = getWeekNumber(startYear, startMonth, startDay)

					var endYear = endFriday.getFullYear();
					var endMonth = endFriday.getMonth() + 1;
					var endDay = endFriday.getDate();
					var endWeekNum = getWeekNumber(endYear, endMonth, endDay)

					//开始的时间是否包含了本周五,如果包含了周五本周数据需要统计，如果不包含周五本周数据不统计
					if(startTime <= startFriday) {

					} else {
						var oneDayLong = 24 * 60 * 60 * 1000;
						var nextFriday = new Date(startFriday.getTime() + 7 * oneDayLong)
						var nextFridayYear = nextFriday.getFullYear();
						var nextFridayMonth = nextFriday.getMonth() + 1;
						var nextFridayDay = nextFriday.getDate();
						startWeekNum = getWeekNumber(nextFridayYear, nextFridayMonth, nextFridayDay);
						startYear = nextFridayYear
					};

					//结束的时间是否包含周五，如果包含周五，这周五的数据需要统计，如果不包含，这周五数据不统计
					if(endTime < endFriday) {
						var oneDayLong = 24 * 60 * 60 * 1000;
						var lastFriday = new Date(endFriday.getTime() - 7 * oneDayLong)
						var lastFridayYear = lastFriday.getFullYear();
						var lastFridayMonth = lastFriday.getMonth() + 1;
						var lastFridayDay = lastFriday.getDate();
						endWeekNum = getWeekNumber(lastFridayYear, lastFridayMonth, lastFridayDay);
						endYear = lastFridayYear
					} else {

					}

					console.log(endTime)
					console.log(endFriday)
					//如果操作完成之后出现起始周大于结束周的情况，则当前周期内不包含周五，无法总计出任何数据
					if(startWeekNum > endWeekNum) {
						$.toast("查询周期无效", "cancel", function(toast) {});
					} else {
						//出现跨年的情况，如果起始年大于结束年，则当前周期内不包含周五，同样无法统计处任何数据
						if(startYear > endYear) {
							$.toast("查询周期无效", "cancel", function(toast) {});
						} else {
							_this.startYear = startYear;
							_this.startWeek = startWeekNum;
							_this.endYear = endYear;
							_this.endWeek = endWeekNum;
							location.href = "statisticsTotalNew.html?startYear=" + _this.startYear + "&startWeek=" + _this.startWeek + "&endYear=" + _this.endYear + "&endWeek=" + _this.endWeek + "&userid=" + _this.userid
						}
					}

				}
			} else {
				$.toast("查询周期无效", "cancel", function(toast) {});
			}
		},
		//点击筛选键功能
		recentWeek: function() {
			var beginDate = getFirstAndLastDay(_this.fridayObj, "line").begin;
			var endDate = getFirstAndLastDay(_this.fridayObj, "line").end;
			_this.firstDate = beginDate;
			_this.lastDate = endDate;
			console.log(_this.fridayObj);
		},
		//本周快捷键
		lastMonth: function() {
			var time = new Date();
			var year = time.getFullYear();
			var mouth = time.getMonth();
			var day = time.getDate()
			var beginDate = year + "-" + ((mouth > 9) ? mouth : "0" + mouth) + "-" + "01"
			var endDate = year + "-" + ((mouth > 9) ? mouth : "0" + mouth) + "-" + getMonthDays(year, mouth - 1)
			_this.firstDate = beginDate;
			_this.lastDate = endDate;
		},
		//上一月快捷键
		recentMouth: function() {
			var time = new Date();
			var year = time.getFullYear();
			var mouth = time.getMonth() + 1;
			var day = time.getDate()
			var beginDate = year + "-" + ((mouth > 9) ? mouth : "0" + mouth) + "-" + "01"
			var endDate = year + "-" + ((mouth > 9) ? mouth : "0" + mouth) + "-" + getMonthDays(year, mouth - 1)
			_this.firstDate = beginDate;
			_this.lastDate = endDate;
		},
		//近一月快捷键
		lastYear: function() {
			var time = new Date();
			var lastYear = time.getFullYear() - 1;
			var beginDate = lastYear + "-01-01"
			var endDate = lastYear + "-12-31"
			_this.firstDate = beginDate;
			_this.lastDate = endDate;
		},
		//上一年
		lastWeek: function() {
			var nowFridayTime = _this.fridayObj.getTime();
			var oneDayLong = 24 * 60 * 60 * 1000;
			var lastFriday = nowFridayTime - 7 * oneDayLong;
			var lastObj = new Date(lastFriday);
			var beginDate = getFirstAndLastDay(lastObj, "line").begin;
			var endDate = getFirstAndLastDay(lastObj, "line").end;
			_this.firstDate = beginDate;
			_this.lastDate = endDate;
		},
		//上一周快捷键
		resetSelect: function() {
			_this.firstDate = "";
			_this.lastDate = "";
			$(".selectFastBtn div").removeClass("active");
		}
	}
})

function getUrlItem(name) { //id
	var url = location.search
	start = url.indexOf(name + "=")
	if(start != -1) {
		start = start + name.length + 1
		end = url.indexOf("&", start)
		if(end == -1) {
			end = url.length
			return unescape(url.substring(start, end))
		}
		return unescape(url.substring(start, end))
	}
	return '';
}

//多周工程档案汇总
function searchProjectTotal(a) {
	$.ajax({
		type: "post",
		url: "http://47.92.116.158:8099/BhGgjAPI/DataSum.asmx/getProjectArcSum?callback=?",
		data: {
			"year": a.startYear,
			"week": a.startWeek,
			"year2": a.endYear,
			"week2": a.endWeek
		},
		dataType: "json",
		success: function(jsondata) {
			a.projectTotal = jsondata;
			a.projectYuDengJi = jsondata["预登记"]
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {}
	});
}

function searchMapProductTotal(a) {
	$.ajax({
		type: "post",
		url: "http://47.92.116.158:8099/BhGgjAPI/DataSum.asmx/getGraphProductSum?callback=?",
		data: {
			"year": a.startYear,
			"week": a.startWeek,
			"year2": a.endYear,
			"week2": a.endWeek
		},
		dataType: "json",
		success: function(jsondata) {
			a.mapTotal = jsondata;
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {}
	});

}

function searchLineTotal(a) {
	$.ajax({
		type: "post",
		url: "http://47.92.116.158:8099/BhGgjAPI/DataSum.asmx/getLineSum?callback=?",
		data: {
			"year": a.startYear,
			"week": a.startWeek,
			"year2": a.endYear,
			"week2": a.endWeek
		},
		dataType: "json",
		success: function(jsondata) {
			a.lineTotal = jsondata
			a.lineZongBiaoSum = jsondata["总表"]
			a.lineGuanXianSum = jsondata["工业管线"]
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {}
	});

}

function searchSearchFileTotal(a) {
	$.ajax({
		type: "post",
		url: "http://47.92.116.158:8099/BhGgjAPI/DataSum.asmx/getArcSum?callback=?",
		data: {
			"year": a.startYear,
			"week": a.startWeek,
			"year2": a.endYear,
			"week2": a.endWeek
		},
		dataType: "json",
		success: function(jsondata) {
			a.fileSearchTotal = jsondata;
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {}
	});
}

function searchBookTotal(a) {
	$.ajax({
		type: "post",
		url: "http://47.92.116.158:8099/BhGgjAPI/DataSum.asmx/getBookSum?callback=?",
		data: {
			"year": a.startYear,
			"week": a.startWeek,
			"year2": a.endYear,
			"week2": a.endWeek
		},
		dataType: "json",
		success: function(jsondata) {
			a.bookTotal = jsondata
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {}
	});
}

function searchOtherTotal(a) {
	$.ajax({
		type: "post",
		url: "http://47.92.116.158:8099/BhGgjAPI/TempStatic.asmx/getOTHER_Static?callback=?",
		data: {
			"year": a.startYear,
			"week": a.startWeek,
			"year2": a.endYear,
			"week2": a.endWeek
		},
		dataType: "json",
		success: function(jsondata) {
			a.otherTotal = jsondata
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {}
	});

}

function searchFileTreeTotal(a) {
	$.ajax({
		type: "post",
		url: "http://47.92.116.158:8099//BhGgjAPI/DataSum.asmx/getArcTotalSum?callback=?",
		data: {
			"year": a.startYear,
			"week": a.startWeek,
			"year2": a.endYear,
			"week2": a.endWeek
		},
		dataType: "json",
		success: function(jsondata) {
			a.fileTreeTotal = jsondata
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {}
	});

}




